package org.esky.properties;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

/**
 * @author Jack.Xu
 * @ClassName: PropertiesFactory
 * @Description: TODO
 * @author: Jack.Xu
 * @date: 2013年10月23日 下午4:41:12
 */
public class PropertiesUtils {
	// 1.使用java.util.Properties类的load()方法
	public static Properties getProperties(InputStream is) throws IOException {
		Properties properties = new Properties();
		properties.load(is);
		return properties;
	}

	public static void readProperties(Properties properties) {
		// 迭代所有值
		Enumeration<?> enumeration = properties.keys();
		while (enumeration.hasMoreElements()) {
			String key = (String) enumeration.nextElement();
			String Property = properties.getProperty(key);
			System.out.println(key + ":" + Property);
		}
	}

	// 2.使用java.util.PropertyResourceBundle类的构造函数
	public static ResourceBundle getResourceBundle(InputStream is) throws IOException {
		ResourceBundle resourceBundle = new PropertyResourceBundle(is);
		return resourceBundle;
	}

	public static void readResourceBundle(ResourceBundle resourceBundle) {
		// 迭代所有值
		Enumeration<?> enumeration = resourceBundle.getKeys();
		while (enumeration.hasMoreElements()) {
			String key = (String) enumeration.nextElement();
			String Property = (String) resourceBundle.getObject(key);
			System.out.println(key + ":" + Property);
		}
	}
}
